﻿$(function () {
    var webForm1Tree = $("#WebForm1Tree");

    webForm1Tree.jstree({
        themes: {
            theme: "classic",
            dots: false,
            icons: false
        },
        core: {
            animation: 0
        },

        callback: {
    },

    json_data: {
        ajax: {
            url: "http://localhost:54304/services/LayoutService.asmx/layoutJsTreeForItemPath",
            type: "POST",
            contentType: "application/json",
            dataType: "json",
            async: "true",
            data: function (n) {
                var result = "{'itemPath':'" + (n.attr ? n.attr("id").replace("node_", "") : "-1") + "'}";
                return (result);
            },
            success: function (data) {
            }
        }
    },

    plugins: ["themes", "json_data", "ui", "core", "crrm", "contextmenu"]
    });
    
    //#events hook

    webForm1Tree.bind("before.jstree",function (event, data){
        if (data.func == "remove"){
        }        
    });

    webForm1Tree.bind("create.jstree", function (event, position, type, callback, is_loaded) {
        var newNode = position.rslt;
        var newNodeName = newNode.name;

        var newNodeParent = position.rslt.parent;
        var parentId = newNodeParent.attr("id");
        var parentIdReplaced = parentId.replace("rnode_", "");
        
        
        $.ajax({
            url: "http://localhost:54304/services/LayoutService.asmx/insertItemAtPosition",
            type: "POST",
            contentType: "application/json",
            dataType: "json",
            async: "true",
            data: "{'parentPath':'"+parentId+"','itemName':'"+newNodeName+"','position':'"+0+"'}",
            success: function(r) {
                alert("created " + newNodeName + " in:" + parentIdReplaced);
            }
        });


    });
    
    webForm1Tree.bind("rename.jstree", function (event, data){
        $.ajax({
            url: "http://localhost:54304/services/LayoutService.asmx/renameItemForPath",
            type: "POST",
            contentType: "application/json",
            dataType: "json",
            async: "true",
            data: "{'itemPath':'"+ data.rslt.obj.attr("id")+"','newName':'"+data.rslt.new_name+"'}",
            success: function(r) {
            }
        });
        
    });


    webForm1Tree.bind("select_node.jstree", function (e, data) {
        var id = data.rslt.obj.attr("id");
    });

    webForm1Tree.bind("remove.jstree", function (event, data) {
        data.rslt.obj.each(function () {
        $.ajax({
            url: "http://localhost:54304/services/LayoutService.asmx/deleteLayoutItemPath",
            type: "POST",
            contentType: "application/json",
            dataType: "json",
            async: "true",
            data: "{'itemPath':'"+ data.rslt.obj.attr("id")+"'}",
            success: function(r) {
            }
        });
        });
    });

});
